<template>
  <div>
    <header-title :header-title="title" />
    <customer-manage-form @on-success-valid="handleSubmit" />
    <el-button
      type="primary"
      style="margin-bottom: 10px"
      @click="handleClick"
    >
      新增客户
    </el-button>
    <customer-manage-dialog
      :row="row"
      :dialog-visible="visible"
      @cancel-dialog="handleCancelDialog"
      @confirm-dialog="handleConfirmDialog"
    />
    <confirm-dialog
      :dialog-visible="confirmVisible"
      @cancel-dialog="handleCancel"
      @confirm-dialog="handleConfirm"
    />
    <el-table
      :data="CustomerManageTableData()"
      border
      stripe
      style="width: 100%"
    >
      <!-- <el-table-column
        type="index"
        :index="getIndex"
        label="序列"
        width="80"
        align="center"
      /> -->
      <el-table-column
        prop="pnumber"
        label="客户编号"
        min-width="100"
        align="center"
      />
      <el-table-column
        prop="name"
        label="客户姓名"
        min-width="130"
        align="center"
      />
      <el-table-column
        prop="tel"
        label="电话号码"
        min-width="150"
        align="center"
      />
      <el-table-column
        prop="idcard"
        label="身份证号码"
        min-width="180"
        align="center"
      />
      <!-- <el-table-column
        prop="sex"
        label="性别"
        min-width="60"
        align="center"
      /> -->
      <el-table-column
        label="性别"
        min-width="60"
        align="center"
      >
        <template slot-scope="scope">
          <span>
            {{ scope.row.sex === '0' ? '女' : '男' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="ctime"
        label="创建日期"
        min-width="180"
        align="center"
      />
      <el-table-column
        label="操作"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            plain
            @click="handleClick(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="CustomerManageDataCount()"
      @click-page="handleClickPage"
      @change-pagesize="handleSizeChange"
    />
  </div>
</template>
<script>
import HeaderTitle from '@/components/HeaderTitle'
import Pagination from '@/components/Pagination'
import CustomerManageForm from '@/components/CustomerManageForm'
import CustomerManageDialog from '@/components/CustomerManageDialog'
import ConfirmDialog from '@/components/ConfirmDialog'
import mixin from '@/mixin/mixin'
export default {
  components: {
    HeaderTitle,
    Pagination,
    CustomerManageForm,
    CustomerManageDialog,
    ConfirmDialog
  },
  mixins: [mixin],
  data() {
    return {
      title: '客户管理',
      page: 1,
      pagesize: 10,
      row: {},
      visible: false,
      confirmVisible: false
    }
  },
  mounted() {
    this.getCustomerManageData({})
  },
  methods: {
    getIndex(index) {
      const newIndex = index + (this.page - 1) * this.pagesize + 1
      if (newIndex < 10) {
        return `00${index + (this.page - 1) * this.pagesize + 1}`
      } else if (newIndex < 100) {
        return `0${index + (this.page - 1) * this.pagesize + 1}`
      } else {
        return index + (this.page - 1) * this.pagesize + 1
      }
    },
    handleSubmit(playload) {
      console.log(playload)
    },
    handleClick(row) {
      let newRow = {}
      if (row.id) {
        newRow = { ...row }
      }
      this.row = newRow
      this.visible = true
    },
    handleClickPage(page) {
      this.page = page
    },
    handleSizeChange(pagesize) {
      this.pagesize = pagesize
    },
    handleCancelDialog() {
      this.visible = false
    },
    handleConfirmDialog(playload) {
      this.visible = false
      this.confirmVisible = true
      console.log(playload)
    },
    handleCancel() {
      this.confirmVisible = false
      this.visible = true
    },
    handleConfirm() {
      this.confirmVisible = false
    }
  }
}
</script>

